﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using NC.Infrastructure.Domain.News;
using NC.Infrastructure.DomainDto.Identity;
using NC.Service.Apply;
using NC.Service.Person;

namespace NC.Web.Areas.Admin.Controllers
{
    /// <summary>
    /// 接口申请控制器
    /// </summary>
    public class ApplyController : Controller
    {
        private readonly IApplyService _applyService;
        private readonly IPersonService _personService;
        
        public ApplyController(IApplyService applyService, IPersonService personService)
        {
            this._applyService = applyService;
            this._personService = personService;
        }


        // GET: Admin/Apply

        public ActionResult Index(int audit = -1, int page = 0, string search = "", int inSchoolTime = 0)
        {
            return View(new List<ApplyPersonListDto>());
        }

        public ActionResult Details(int id)
        {
            return View(new ApplyPersonDto());
        }

        // POST: Apply/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult Delete(string id)
        {

            if (string.IsNullOrEmpty(id))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            string[] ids = id.Split('|');
            int _id;
            foreach (var item in ids)
            {
                _id = Convert.ToInt32(item);
                var _entity = _applyService.Get(_id);
                _applyService.Delete(_entity);
            }
            _applyService.Commit();
            return Json("", JsonRequestBehavior.AllowGet);
        }


        #region Api
        /// <summary>
        /// 将申请设置为正在审核中的状态
        /// </summary>
        /// <param name="id">申请Id</param>
        /// <returns>申请返回数据</returns>
        public JsonResult Applying(int id)
        {
            var apply = _applyService.Get(id);
            if (apply == null)
                return Json(new { status = 0, msg = "没有此申请" });
            apply.Audit = ApplyStatus.Appling;
            _applyService.Commit();
            return Json(new { status = 1 });
        }
        /// <summary>
        /// 将申请设置为正在已审核的状态
        /// </summary>
        /// <param name="id">申请Id</param>
        /// <returns>申请返回数据</returns>
        public JsonResult Applied(int id)
        {
            var apply = _applyService.Get(id);
            if (apply == null)
                return Json(new { status = 0, msg = "没有此申请" });
            apply.Audit = ApplyStatus.Applied;
            _applyService.Commit();
            return Json(new { status = 1 });
        }


        #endregion


    }
}